#define store_x sw
#define load_x lw
#define portWORD_SIZE 4
#define portCONTEXT_SIZE ( 31 * portWORD_SIZE )
addi sp, sp, -portCONTEXT_SIZE
.section .init
.global main

j start
j .
trap_entry:
j trap
j dbg_monitor
dbg_monitor:
	la a0,0xff00
dbg_monitor_wait:
	lw a1,0(a0)
	beq a1,x0,dbg_monitor_wait
	sw x0,0(a0)
	csrrw a0,dcsr,x0
	la a1,0x40
	or a0,a0,a1
	csrrw x0,dcsr,a0
	addi a0,x0,0
	csrrw x0,mcause,a0
	dret
start:
//.word 0x00000000
//pc == 0xc
//ebreak
la a0,0xff00
sw x0,0(a0)
csrrw x1,mbadaddr,x2
sfence.vma
la x1,-2
la x2,-1
la x5,0xffffffff
la x6,0xfffffff0
slt x3,x2,x1
sltu x4,x6,x5
la sp,_eusrstack

 //csrrw x0,mtvec, a0
 la t0, main
 //jal main
 csrw mepc, t0
 uret
 mret
 j .
jal main

trap:
	addi x2,x0,0
	csrrw x1,mcause,x2
	csrrw x3,mepc,x0
	sw x1,240(x0)
	sw x3,240(x0)
	j .
	ecall
	ebreak
	mret
	fence
	fence.i
	sfence.vma
	csrrw x1,dcsr,x2
	csrrw x1,satp,x2
	csrrw x1,dpc,x2
	csrrw x1,sepc,x2
	sret
	dret
    addi sp, sp, -portCONTEXT_SIZE
    store_x x1, 0 * portWORD_SIZE( sp )
    store_x x2, 1 * portWORD_SIZE( sp )
	store_x x3, 2 * portWORD_SIZE( sp )
	store_x x4, 3 * portWORD_SIZE( sp )
	store_x x5, 4 * portWORD_SIZE( sp )
	store_x x6, 5 * portWORD_SIZE( sp )
	store_x x7, 6 * portWORD_SIZE( sp )
	store_x x8, 7 * portWORD_SIZE( sp )
	store_x x9, 8 * portWORD_SIZE( sp )
	store_x x10, 9 * portWORD_SIZE( sp )
	store_x x11, 10 * portWORD_SIZE( sp )
	store_x x12, 11 * portWORD_SIZE( sp )
	store_x x13, 12 * portWORD_SIZE( sp )
	store_x x14, 13 * portWORD_SIZE( sp )
	store_x x15, 14 * portWORD_SIZE( sp )
	store_x x16, 15 * portWORD_SIZE( sp )
	store_x x17, 16 * portWORD_SIZE( sp )
	store_x x18, 17 * portWORD_SIZE( sp )
	store_x x19, 18 * portWORD_SIZE( sp )
	store_x x20, 19 * portWORD_SIZE( sp )
	store_x x21, 20 * portWORD_SIZE( sp )
	store_x x22, 21 * portWORD_SIZE( sp )
	store_x x23, 22 * portWORD_SIZE( sp )
	store_x x24, 23 * portWORD_SIZE( sp )
	store_x x25, 24 * portWORD_SIZE( sp )
	store_x x26, 25 * portWORD_SIZE( sp )
	store_x x27, 26 * portWORD_SIZE( sp )
	store_x x28, 27 * portWORD_SIZE( sp )
	store_x x29, 28 * portWORD_SIZE( sp )
    store_x x30, 29 * portWORD_SIZE( sp )
    store_x x31, 30 * portWORD_SIZE( sp )
    //csrrw sp,mstatus,sp
    //csrrw a0,mcause,x0
    //csrrw a1,mepc,x0
	addi a2,sp,0
    call irq
    addi a0,x0,0
    //csrrw x0,mcause,a0//清中断
    //csrrw sp,mstatus,sp
    load_x  x1, 0 * portWORD_SIZE( sp )
    load_x  x2, 1 * portWORD_SIZE( sp )
	load_x  x3, 2 * portWORD_SIZE( sp )		/* t0 */
	load_x  x4, 3 * portWORD_SIZE( sp )		/* t1 */
	load_x  x5, 4 * portWORD_SIZE( sp )		/* t2 */
	load_x  x6, 5 * portWORD_SIZE( sp )		/* s0/fp */
	load_x  x7, 6 * portWORD_SIZE( sp )		/* s1 */
	load_x  x8, 7 * portWORD_SIZE( sp )	/* a0 */
	load_x  x9, 8 * portWORD_SIZE( sp )	/* a1 */
	load_x  x10, 9 * portWORD_SIZE( sp )	/* a2 */
	load_x  x11, 10 * portWORD_SIZE( sp )	/* a3 */
	load_x  x12, 11 * portWORD_SIZE( sp )	/* a4 */
	load_x  x13, 12 * portWORD_SIZE( sp )	/* a5 */
	load_x  x14, 13 * portWORD_SIZE( sp )	/* a6 */
	load_x  x15, 14 * portWORD_SIZE( sp )	/* a7 */
	load_x  x16, 15 * portWORD_SIZE( sp )	/* s2 */
	load_x  x17, 16 * portWORD_SIZE( sp )	/* s3 */
	load_x  x18, 17 * portWORD_SIZE( sp )	/* s4 */
	load_x  x19, 18 * portWORD_SIZE( sp )	/* s5 */
	load_x  x20, 19 * portWORD_SIZE( sp )	/* s6 */
	load_x  x21, 20 * portWORD_SIZE( sp )	/* s7 */
	load_x  x22, 21 * portWORD_SIZE( sp )	/* s8 */
	load_x  x23, 22 * portWORD_SIZE( sp )	/* s9 */
	load_x  x24, 23 * portWORD_SIZE( sp )	/* s10 */
	load_x  x25, 24 * portWORD_SIZE( sp )	/* s11 */
	load_x  x26, 25 * portWORD_SIZE( sp )	/* t3 */
	load_x  x27, 26 * portWORD_SIZE( sp )	/* t4 */
	load_x  x28, 27 * portWORD_SIZE( sp )	/* t5 */
	load_x  x29, 28 * portWORD_SIZE( sp )	/* t6 */
    load_x  x30, 29 * portWORD_SIZE( sp )	/* t6 */
    load_x  x31, 30 * portWORD_SIZE( sp )	/* t6 */
    addi sp, sp, portCONTEXT_SIZE
    mret

